﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace hozorghiab
{
    public partial class release : Form
    {
        public sql sqlquery;
        DataTable table;
        SqlDataAdapter da;
        DataGridViewButtonColumn deleteButton, editeButton;
        BindingSource bs = new BindingSource();
        AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
        AutoCompleteStringCollection familyCollection = new AutoCompleteStringCollection();
        bool load = false;
        public release(sql s)
        {
            InitializeComponent();
            sqlquery = s;
        }

        void setheader()
        {
            dataGridView1.Columns[0].Visible = false;
            dataGridView1.ReadOnly = true;
            dataGridView1.AllowUserToAddRows = false;
        }
        public bool calc_release(int empid)
        {
            int count = 30;
            SqlDataReader dReader;
            
            sqlquery.open();
            dReader = sqlquery.ExecuteQuery("Select distinct [employeid] from vacation where totalday=true and @empid=employeid");
            while (dReader.Read())
                count--;
            sqlquery.close();
            if (count == 0)
                return false;
            else
                return true;
        }

        private void release_Load(object sender, EventArgs e)
        {
            SqlDataReader dReader;
            sqlquery.open();
            dReader = sqlquery.ExecuteQuery("Select distinct [Name] from profile order by [Name] asc");

            if (dReader.HasRows == true)
            {
                while (dReader.Read())
                    namesCollection.Add(dReader["Name"].ToString().Trim());
            }
            else
            {
                MessageBox.Show("Data not found");
            }
            dReader.Close();

            dReader = sqlquery.ExecuteQuery("Select distinct [family] from profile order by [family] asc");
            if (dReader.HasRows == true)
            {
                while (dReader.Read())
                    familyCollection.Add(dReader["family"].ToString().Trim());
            }
            else
            {
                MessageBox.Show("Data not found");
            }
            dReader.Close();
            textBox10.AutoCompleteMode = AutoCompleteMode.Suggest;
            textBox10.AutoCompleteSource = AutoCompleteSource.CustomSource;
            textBox10.AutoCompleteCustomSource = namesCollection;

            textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
            textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            textBox1.AutoCompleteCustomSource = namesCollection;

            textBox13.AutoCompleteMode = AutoCompleteMode.Suggest;
            textBox13.AutoCompleteSource = AutoCompleteSource.CustomSource;
            textBox13.AutoCompleteCustomSource = familyCollection;

            textBox2.AutoCompleteMode = AutoCompleteMode.Suggest;
            textBox2.AutoCompleteSource = AutoCompleteSource.CustomSource;
            textBox2.AutoCompleteCustomSource = familyCollection;
            sqlquery.close();

            //persianDateTimePicker2.Value = PersianDate.Now;
        }

     
        private void save_option_Click(object sender, EventArgs e)
        {

        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void save_btn_Click_1(object sender, EventArgs e)
        {
            string q;
            try
            {
                sqlquery.open();
                int empid = -1;
                empid = (int)sqlquery.ExecuteScalar("SELECT employeid FROM profile where profile.name =N'" + textBox10.Text.Trim() + "' and profile.family=N'" + textBox13.Text.Trim() + "'");
                if (calc_release(empid) == false)
                    MessageBox.Show("تعداد مرخصی ها به اتمام رسیده است ");
                else
                {

                    if (checkBox1.Checked == true)
                    {
                        q = "INSERT INTO vacation (employeid,day,month,year,beginhour,beginmin,endhour,endmin,totalday,code)VALUES(" + empid.ToString().Trim() + "," + persianDateTimePicker2.Value.Day.ToString() + "," + persianDateTimePicker2.Value.Month.ToString() + "," + persianDateTimePicker2.Value.Year.ToString() + "," + "Null" + "," + "Null" + "," + "Null" + "," + "Null" + ",1," + codetextbox.Text + ")";

                    }
                    else
                        q = "INSERT INTO vacation (employeid,day,month,year,beginhour,beginmin,endhour,endmin,totalday,code)VALUES(" + empid.ToString().Trim() + "," + persianDateTimePicker2.Value.Day.ToString() + "," + persianDateTimePicker2.Value.Month.ToString() + "," + persianDateTimePicker2.Value.Year.ToString() + "," + beginhournumeric.Value + "," + beginminnumeric.Value + "," + endhournumeric.Value + "," + endminnumeric.Value + ",0," + codetextbox.Text + ")";
                    sqlquery.ExecuteNonQuery(q);
                    sqlquery.close();
                }
            }
            catch
            {
                MessageBox.Show("لطفا اطلاعات را درست وارد کنید");
            }
            textBox10.Text = "";
            textBox13.Text = "";
            codetextbox.Text = "";
            checkBox1.Checked = false;

        }

        private void show_option_Click(object sender, EventArgs e)
        {
            string q;
            try
            {
                sqlquery.open();
                if (textBox1.Text.Length > 0 || textBox2.Text.Length > 0)
                    q = "SELECT id,vacation.employeid as[شماره کارمندی],rtrim(name)+space(1)+rtrim(family) as [نام و نام خانوادگی],ltrim(str(year))+'/'+ltrim(str(month))+'/'+ltrim(str(day)) as [تاریخ],ltrim(str(beginhour))+':'+ltrim(str(beginmin)) as [ساعت شروع],ltrim(str(endhour))+':'+ltrim(str(endmin)) as [ساعت پایان],totalday as [کل روز],code as[نوع مرخصی] FROM vacation,profile WHERE profile.employeid=vacation.employeid and vacation.employeid IN (SELECT profile.employeid FROM profile WHERE name=N'" + textBox1.Text.Trim() + "' or family=N'" + textBox2.Text.Trim() + "' )";
                else
                    q = "SELECT TOP 150 id,vacation.employeid as[شماره کارمندی],rtrim(name)+space(1)+ltrim(family) as [نام و نام خانوادگی],ltrim(str(year))+'/'+ltrim(str(month))+'/'+ltrim(str(day)) as [تاریخ],ltrim(str(beginhour))+':'+ltrim(str(beginmin)) as [ساعت شروع],ltrim(str(endhour))+':'+ltrim(str(endmin)) as [ساعت پایان],totalday as [کل روز],code as[نوع مرخصی] FROM vacation,profile WHERE profile.employeid=vacation.employeid";
                da = sqlquery.sqlcommand(q);
                table = new DataTable();
                table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                da.Fill(table);
                bs.DataSource = table;
                dataGridView1.DataSource = bs;
                sqlquery.close();
            }
            catch
            {
                MessageBox.Show("مشکلی رخ داده است");
            }
            //setheader();
            id_txtbox.Text = "";
        }

        private void id_txtbox_TextChanged(object sender, EventArgs e)
        {
            sqlquery.open();
            da = sqlquery.sqlcommand("SELECT * FROM vacation WHERE id like '%" + id_txtbox.Text.Trim() + "%'");
            table = new DataTable();
            da.Fill(table);
            bs.DataSource = table;
            dataGridView1.DataSource = bs;
            sqlquery.close();
        }

        private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {
            int currentRow = int.Parse(e.RowIndex.ToString());

            if (dataGridView1.Columns[e.ColumnIndex] == deleteButton && currentRow >= 0)
            {
                DialogResult msg = MessageBox.Show("Are You Sure?", "Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (msg == DialogResult.OK)
                {
                    sqlquery.open();
                    sqlquery.ExecuteNonQuery("DELETE vacation WHERE id=" + dataGridView1[2, currentRow].Value);
                    sqlquery.close();
                    dataGridView1.Rows.RemoveAt(currentRow);
                }
            }
            else if (dataGridView1.Columns[e.ColumnIndex] == editeButton && currentRow >= 0)
            {
                editerelease edform = new editerelease(sqlquery, dataGridView1[2, currentRow].Value.ToString(), dataGridView1[3, currentRow].Value.ToString(), dataGridView1[4, currentRow].Value.ToString(), dataGridView1[5, currentRow].Value.ToString(), dataGridView1[6, currentRow].Value.ToString(), dataGridView1[7, currentRow].Value.ToString(), dataGridView1[8, currentRow].Value.ToString(), dataGridView1[9, currentRow].Value.ToString());
                edform.Show();
            }
        }

        private void dataGridView1_DataBindingComplete_1(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (!load)
            {

                editeButton = new DataGridViewButtonColumn();
                editeButton.HeaderText = "ویرایش";
                editeButton.Text = "ویرایش";
                editeButton.UseColumnTextForButtonValue = true;
                editeButton.Width = 80;
                dataGridView1.Columns.Add(editeButton);

                deleteButton = new DataGridViewButtonColumn();
                deleteButton.HeaderText = "حذف";
                deleteButton.Text = "حذف";
                deleteButton.UseColumnTextForButtonValue = true;
                deleteButton.Width = 80;
                dataGridView1.Columns.Add(deleteButton);
                setheader();
                load = true;
            }
        }

        private void checkBox1_CheckedChanged_1(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                beginhournumeric.Enabled = false;
                beginminnumeric.Enabled = false;
                endhournumeric.Enabled = false;
                endminnumeric.Enabled = false;
            }
            else
            {
                beginhournumeric.Enabled = true;
                beginminnumeric.Enabled = true;
                endhournumeric.Enabled = true;
                endminnumeric.Enabled = true;
            }
        }



    }
}
